package com.link.base.base.system.controller;


import com.link.base.base.system.model.Period;
import com.link.base.base.system.service.PeriodService;
import com.link.core.cllog.LCLogger;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.ServiceException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 期间
 * @author unknown
 * @date unknown
 */
@Controller
@RequestMapping("/link/period")
public class PeriodController extends BasicController<Period> {

    @Resource
    private PeriodService periodService;

    @Override
    public BasicService<Period> getBasicService() throws Exception {
        return periodService;
    }

    /**
     * 根据年份查询月期间
     *
     * @author unknown
     * @date unknown
     */
    @RequestMapping(value = "/getMonthPeriod")
    @ResponseBody
    public Map<String, Object> getMonthPeriod(HttpServletResponse response, HttpServletRequest request) throws Exception {
        Map<String, Object> result = new HashMap<String, Object>(16);
        try {
            Period period = new Period();
            List<Period> periodList = periodService.getMonthPriod(period);
            result.put("result", periodList);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("result", e.getMessage());
            result.put("success", false);
        }
        return result;
    }

    /**
     * 根据日期查询所属月份期间
     *
     * @author unknown
     * @date unknown
     */
    @RequestMapping(value = "/queryMonthPeriodByDate")
    @ResponseBody
    public Map<String, Object> queryMonthPeriodByDate(HttpServletResponse response, HttpServletRequest request, Period period) throws Exception {
        Map<String, Object> result = new HashMap<String, Object>(16);
        try {
            Period entity = new Period();
            entity.setClosed("Y");
            entity.setType("Month");
            entity.setDate(period.getDate());
            List<Period> periodList = periodService.queryMonthPeriodByDate(entity);
            Period record = new Period();
            if (periodList != null && periodList.size() == 1) {
                record = periodList.get(0);
            } else {
                throw new ServiceException("SYSTEM-001");
            }
            if (record == null) {
                throw new ServiceException("SYSTEM-002");
            }
            result.put("result", record);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("result", e.getMessage());
            result.put("success", false);
        }
        return result;
    }
}
